//
//  MCLotteryHallViewController.m
//  TLYL
//
//  Created by miaocai on 2017/6/1.
//  Copyright © 2017年 TLYL01. All rights reserved.
//

#import "MCLotteryHallViewController.h"
#import "MCPickNumberViewController.h"
#import "MCUserDefinedLotteryCategoriesViewController.h"
#import "MCHomePageLotteryCategoriesView.h"
#import "MCHomePagePictrueCycleView.h"
#import "MCHomePageInfoCycleView.h"
#import "MCPickNumberViewController.h"
#import "MCLotteryID.h"
#import "MCCZTXView.h"
#import "MCUserDefinedLotteryCategoriesModel.h"
#import "MCWithdrawRecDeltailViewController.h"
#import "MCRechargeViewController.h"
#import "MCUserMoneyModel.h"
#import "MCMineInfoModel.h"
#import "MCHasPayPwdModel.h"
#import "MCDataTool.h"
#import "MCGetLotteryCustomModel.h"
#import "MCSetLotteryCustomModel.h"
#import "MCSystemNoticeDetailViewController.h"
#import "MCSystemNoticeViewController.h"
#import "UIView+MCParentController.h"
#import "MCKefuViewController.h"
#import "MCMineAddBankCardViewController.h"
#import "MCPasswordSettingViewController.h"

@interface MCLotteryHallViewController ()<MCHomePageLotteryCategoriesDelegate>

//自定义view
@property(nonatomic,weak)MCHomePageLotteryCategoriesView *lotteryView;
//money_model模型的强引用
@property(nonatomic,strong)MCUserMoneyModel * money_model;
//mineInfo_model模型的强引用
@property(nonatomic,strong)MCMineInfoModel *mineInfoModel;
//hasPayPwd_model模型的强引用
@property(nonatomic,strong)MCHasPayPwdModel *hasPayPwdModel;

@end

@implementation MCLotteryHallViewController

#pragma mark-life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"天利娱乐";
    [self setUpUI];
    [self loadDefaultCZList];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadDefaultCZList) name:@"MCloadDefaultCZList" object:nil];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
  
    self.navigationController.navigationBarHidden=NO;
    self.navigationController.navigationBar.translucent = NO;
    [self reloadData];


}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];

}
#pragma mark-setUpUI
- (void)setUpUI{
    self.view.backgroundColor = RGB(239, 239, 239);
    CGFloat naviHeight = 0;
    CGFloat picCycleHeight = MC_REALVALUE(150);

    /*
     * 九宫格  彩种
     */
    MCHomePageLotteryCategoriesView *lotteryView = [[MCHomePageLotteryCategoriesView alloc] initWithFrame:CGRectMake(0, naviHeight + picCycleHeight + MC_REALVALUE(80) - 10, G_SCREENWIDTH, G_SCREENHEIGHT - naviHeight - picCycleHeight - MC_REALVALUE(80) - 49)];
    lotteryView.delegate=self;
    _lotteryView=lotteryView;
    __weak MCLotteryHallViewController *weakSelf = self;
    lotteryView.collectionViewDidSelectedCallBack = ^(MCUserDefinedLotteryCategoriesModel *model){
        MCPickNumberViewController *pickVC =  [[MCPickNumberViewController alloc] init];
        pickVC.lotteriesTypeModel = model;
        [weakSelf.navigationController pushViewController:pickVC animated:YES];
    };
    [self.view addSubview:lotteryView];
    
    
    
    /*
     * 轮播
     */
    MCHomePagePictrueCycleView *picView = [[MCHomePagePictrueCycleView alloc] initWithFrame:CGRectMake(0, naviHeight, G_SCREENWIDTH, picCycleHeight)];
    [self.view addSubview:picView];
    
    /*
     * 3S广告通知栏
     */
    MCHomePageInfoCycleView *infoView = [[MCHomePageInfoCycleView alloc] initWithFrame:CGRectMake(0, picCycleHeight + naviHeight, G_SCREENWIDTH, MC_REALVALUE(30))];
    [self.view addSubview:infoView];
    infoView.systemNoticeClickBlock = ^(MCSystemNoticeListModel *model) {
        
//        self.tabBarController.selectedIndex = 2;
        
        [UIView animateWithDuration:0.001 animations:^{
            
            MCSystemNoticeViewController * vc=[[MCSystemNoticeViewController alloc]init];
            [[UIView MCcurrentViewController].navigationController pushViewController:vc animated:NO];
            
        } completion:^(BOOL finished) {
                    MCSystemNoticeDetailViewController * vc=[[MCSystemNoticeDetailViewController alloc]init];
                    vc.NewsID=model.MerchantNews_ID;
                    [[UIView MCcurrentViewController].navigationController pushViewController:vc animated:NO];
        }];

        
    };
    
    
    /*
     * 充值 提现 栏
     */
    MCCZTXView *cztxView = [[MCCZTXView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(infoView.frame) + 1, G_SCREENWIDTH, MC_REALVALUE(45))];
    
    cztxView.tixianBtnClickBlock = ^{
        
        BOOL hasCard = [self.mineInfoModel.BankCardNumber length]>1?YES:NO;
        BOOL hasPassword = [self.hasPayPwdModel.PayOutPassWord intValue]==1?YES:NO;
        
        /*
         * 情况一： 无资金密码  无默认银行卡
         */
        if (!hasCard&&!hasPassword) {
            
            //先绑卡-》设置资金密码-》退回我的
            MCMineAddBankCardViewController * vc=[[MCMineAddBankCardViewController alloc]init];
            vc.type=NOPasswordAndNOCard;
            self.navigationController.navigationBarHidden=NO;
            [self.navigationController pushViewController:vc animated:YES];
            
            
            //情况二： 有资金密码  无默认银行卡
        }else if (hasPassword&&!hasCard){
            
            //先绑卡-》退回我的
            MCMineAddBankCardViewController * vc=[[MCMineAddBankCardViewController alloc]init];
            vc.type=HasPasswordAndNOCard;
            self.navigationController.navigationBarHidden=NO;
            [self.navigationController pushViewController:vc animated:YES];
            
            //情况三： 无资金密码  有默认银行卡
        }else if (!hasPassword&&hasCard){
            
            //设置资金密码-》退回我的
            MCPasswordSettingViewController * vc =[[MCPasswordSettingViewController alloc]init];
            self.navigationController.navigationBarHidden=NO;
            [self.navigationController pushViewController:vc animated:YES];
            
            
            //情况四： 有资金密码  有默认银行卡
        }else{
            
            MCWithdrawRecDeltailViewController * vc=[[MCWithdrawRecDeltailViewController alloc]init];
            self.navigationController.navigationBarHidden=NO;
            [self.navigationController pushViewController:vc animated:YES];
        }

        
    };
    
    cztxView.chongzhiBtnClickBlock = ^{
        MCRechargeViewController * vc = [[MCRechargeViewController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    };
    
    [self.view addSubview:cztxView];
    
    
    /*
     * 客服按钮
     */
    UIButton *btn = [[UIButton alloc] init];
    [btn setImage:[UIImage imageNamed:@"kefu"] forState:UIControlStateNormal];
    btn.frame = CGRectMake(0, 0, MC_REALVALUE(30), MC_REALVALUE(30));
    btn.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    [btn addTarget:self action:@selector(contactCustomerServiceBtnClick:) forControlEvents:UIControlEventTouchDown];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    
}

- (void)reloadData{

    
    __weak __typeof(self)wself = self;

    MCUserMoneyModel * money_model=[MCUserMoneyModel sharedMCUserMoneyModel];
    [money_model refreashDataAndShow];
    self.money_model=money_model;
    money_model.callBackSuccessBlock = ^(id manager) {
  
        wself.money_model.FreezeMoney=manager[@"FreezeMoney"];
        wself.money_model.LotteryMoney=manager[@"LotteryMoney"];
    };
    
    MCMineInfoModel *mineInfo_model = [MCMineInfoModel sharedMCMineInfoModel];
    self.mineInfoModel = mineInfo_model;
    [mineInfo_model refreashDataAndShow];
    mineInfo_model.callBackSuccessBlock = ^(id manager) {
      
        wself.mineInfoModel=[MCMineInfoModel mj_objectWithKeyValues:manager];
        [[NSUserDefaults standardUserDefaults] setObject:wself.mineInfoModel.DrawBeginTime forKey:@"DrawBeginTime"];
        [[NSUserDefaults standardUserDefaults] setObject:wself.mineInfoModel.DrawEndTime forKey:@"DrawEndTime"];
    };
    
    MCHasPayPwdModel * hasPayPwd_model=[MCHasPayPwdModel sharedMCHasPayPwdModel];
    [hasPayPwd_model refreashDataAndShow];
    self.hasPayPwdModel=hasPayPwd_model;
    hasPayPwd_model.callBackSuccessBlock = ^(id manager) {
       
        wself.hasPayPwdModel.PayOutPassWord=[NSString stringWithFormat:@"%@",manager[@"PayOutPassWord"]];
    };
    
    
}
#pragma mark-touch event
- (void)contactCustomerServiceBtnClick:(UIButton *)btn{
  MCKefuViewController *vc =  [[MCKefuViewController alloc] init];
    vc.title = @"联系客服";
    [self.navigationController pushViewController:vc animated:YES];

}
/*
 * 跳转自定义彩种
 */
#pragma mark-MCHomePageLotteryCategoriesDelegate
-(void)SetUserDefinedLotteryCategories{
    MCUserDefinedLotteryCategoriesViewController * vc = [[MCUserDefinedLotteryCategoriesViewController alloc]init];
    __block MCLotteryHallViewController * weakSelf = self;
    vc.block=^(NSMutableArray<MCUserDefinedLotteryCategoriesModel *> *marr){
        
        [weakSelf.lotteryView relayOutUI];
        
    };
    
    [self.navigationController pushViewController:vc animated:YES];

}

#pragma mark-加载用户自定义彩种
-(void)loadDefaultCZList{
    
    __weak typeof(self) weakSelf = self;
    
    MCUserDefinedLotteryCategoriesViewController * vc=[[MCUserDefinedLotteryCategoriesViewController alloc]init];
    [vc GetLotteryCustomArray:^(BOOL result, NSMutableArray *defaultCZArray) {
        if (result) {
            [weakSelf.lotteryView relayOutUI];
        }
    }];
    
}

    
@end




